//
//  LBFTabBarController.m
//  Go!
//
//  Created by 凌風 on 16/7/7.
//  Copyright © 2016年 凌风. All rights reserved.
//

#import "LBFTabBarController.h"
#import "LBFTabBar.h"
#import "HQYHomePageViewController.h" // 首页
#import "HQYFoodViewController.h" // 美食
#import "LBFTravelViewController.h" // 旅游
#import "MHCHHotelViewController.h"// 酒店
#import "W_MineViewController.h" // 我的

#import "MHCHChoisCityViewController.h"


@interface LBFTabBarController () <LBFTabBarDelegate>

@property (nonatomic, strong) NSMutableArray *vcArray;

@end

@implementation LBFTabBarController

#pragma mark -- 第一次使用当前类的时候对设置UITabBarItem的主题
+ (void)initialize {
    UITabBarItem *tabBarItem = [UITabBarItem appearanceWhenContainedInInstancesOfClasses:@[self]];
    
    NSMutableDictionary *dictNormal = [NSMutableDictionary dictionary];
    dictNormal[NSForegroundColorAttributeName] = [UIColor grayColor];
    dictNormal[NSFontAttributeName] = [UIFont systemFontOfSize:Font(11)];
    
    NSMutableDictionary *dictSelected = [NSMutableDictionary dictionary];
    dictSelected[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
    dictSelected[NSFontAttributeName] = [UIFont systemFontOfSize:Font(11)];
    
    [tabBarItem setTitleTextAttributes:dictNormal forState:UIControlStateNormal];
    [tabBarItem setTitleTextAttributes:dictSelected forState:UIControlStateSelected];
    
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addAllChildViewController];
    
    //创建自己的tabbar，然后用kvc将自己的tabbar和系统的tabBar替换下
    LBFTabBar *tabbar = [[LBFTabBar alloc] init];
    tabbar.myDelegate = self;
    //kvc实质是修改了系统的_tabBar
    [self setValue:tabbar forKeyPath:@"tabBar"];
}


#pragma mark -- 初始化tabBar上除了home按钮之外所有的按钮
- (void)addAllChildViewController {
    
    self.vcArray = [NSMutableArray array];
    
    // 美食VC
    HQYFoodViewController *firstVc = [[HQYFoodViewController alloc] init];
    [self setUpOneChildVcWithVC:firstVc ImageName:@"美食-未选中" selectedImageName:@"美食-选中" title:@"美食"];
    
    // 旅游VC
    LBFTravelViewController *secVc = [[LBFTravelViewController alloc] init];
    [self setUpOneChildVcWithVC:secVc ImageName:@"旅游-未选中" selectedImageName:@"旅游-选中" title:@"旅游"];
    // 首页
    
    HQYHomePageViewController *otherVc = [HQYHomePageViewController shareHomeVc];
    [self setUpOneChildVcWithVC:otherVc ImageName:@"旅游-未选中" selectedImageName:@"旅游-选中" title:@""];
//    MHCHHotelViewController *otherVc = [[MHCHHotelViewController alloc] init];
//    [self setUpOneChildVcWithVC:otherVc ImageName:@"酒店-未选中" selectedImageName:@"酒店-选中" title:@"酒店"];
//    MHCHChoisCityViewController *otherVc = [[MHCHChoisCityViewController alloc] init];
//    [self setUpOneChildVcWithVC:otherVc ImageName:@"酒店-未选中" selectedImageName:@"酒店-选中" title:@"酒店"];
    // 酒店VC
    MHCHHotelViewController *threeVc = [[MHCHHotelViewController alloc] init];
    [self setUpOneChildVcWithVC:threeVc ImageName:@"酒店-未选中" selectedImageName:@"酒店-选中" title:@"酒店"];
    
    // 我的VC
    W_MineViewController *fouthVc = [[W_MineViewController alloc] init];
    [self setUpOneChildVcWithVC:fouthVc ImageName:@"我的-未选中" selectedImageName:@"我的-选中" title:@"我的"];
    
    self.viewControllers = self.vcArray;
}


- (void)setUpOneChildVcWithVC:(UIViewController *)vc ImageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName title:(NSString *)title {
    
    
    
//    Class class = NSClassFromString(vcName);
    
//    UIViewController *vc = [[class alloc] init];
    
    
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    
    UIImage *normalImage = [UIImage imageNamed:imageName];
    normalImage = [normalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //tabBarItem，是系统提供模型，专门负责tabbar上按钮的文字以及图片展示
    vc.tabBarItem.image = normalImage;
    
    UIImage *selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    vc.tabBarItem.selectedImage = selectedImage;
    
    vc.tabBarItem.title = title;
    
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateSelected];
    
    vc.navigationItem.title = title;
    
//    vc.view.backgroundColor = [self randomColor];
    
//    [self addChildViewController:nav];
    
    [self.vcArray addObject:nav];
}

#pragma mark -- 点击home按钮的代理方法
- (void)tabBarHomeBtnClick:(LBFTabBar *)tabBar {
    
//    UIViewController *homeVc = [[UIViewController alloc] init];
//    
//    homeVc.view.backgroundColor = [self randomColor];
//    
//    UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:homeVc];
    
    self.selectedIndex = 2;
    
}

- (UIColor *)randomColor
{
    CGFloat r = arc4random_uniform(256);
    CGFloat g = arc4random_uniform(256);
    CGFloat b = arc4random_uniform(256);
    return [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
